package com.markyao.mq;
import com.markyao.dao.pojo.MiaoshaOrder;
import com.markyao.dao.pojo.Product;
import com.markyao.dao.pojo.User;
import com.markyao.service.MiaoshaService;
import com.markyao.service.ProductService;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class MqConsumer {
    @Autowired
    ProductService productService;
    @Autowired
    MiaoshaService miaoshaService;
    //mq消费端,主要用于插入秒杀订单,监听秒杀队列
    @RabbitListener(queues = MQConfig.MIAOSHA_QUEUE,messageConverter = "jacksonConverter")
    public void recevie(MiaoshaMessage message){

        User user = message.getUser();
        Integer goodsId = message.getGoodsId();
        //通过商品的id查询此商品此时库存量多少
        Product product=productService.getProductByid(goodsId);
        Integer miaoshaCount = product.getMiaoshaCount();
        if(miaoshaCount<=0){
            return;
        }
        MiaoshaOrder miaoshaOrder = miaoshaService.findOrderByUidAndGoodId(user.getUid(), goodsId);
        if(miaoshaOrder!=null){
            //已经秒杀到了
            return;
        }
        //插入订单之中
        miaoshaService.insertMiaoshaOrder(user,goodsId);
    }
}
